package com.chonor.mall.controller;

import com.chonor.mall.common.CommonResult;
import com.chonor.mall.model.MemberLevel;
import com.chonor.mall.service.MemberLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 会员等级管理Controller
 */
@Controller
@Api(tags = "MemberLevelController", description = "会员等级管理")
@RequestMapping("/memberLevel")
public class MemberLevelController {
    @Autowired
    private MemberLevelService memberLevelService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation("查询所有会员等级")
    @ResponseBody
    public CommonResult<List<MemberLevel>> list() {
        List<MemberLevel> memberLevelList = memberLevelService.list();
        return CommonResult.success(memberLevelList);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ApiOperation("新增会员等级")
    @ResponseBody
    public CommonResult create(@RequestBody MemberLevel memberLevel) {
        int count = memberLevelService.create(memberLevel);
        if(count > 0){
            return CommonResult.success(count);
        }
        return CommonResult.failed("创建会员等级失败");
    }

    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ApiOperation("根据id获取会员等级")
    @ResponseBody
    public CommonResult detail(@PathVariable Long id) {
        if(Objects.isNull(id)) return CommonResult.failed("id不能为空");
        MemberLevel item = memberLevelService.getItem(id);
        return CommonResult.success(item);
    }

    @RequestMapping(value = "/upload/{id}", method = RequestMethod.POST)
    @ApiOperation("更新会员等级")
    @ResponseBody
    public CommonResult upload(@PathVariable Long id, @RequestBody MemberLevel memberLevel) {
        int count = memberLevelService.update(id, memberLevel);
        if(count > 0){
            return CommonResult.success(count);
        }
        return CommonResult.failed("更新会员等级失败");
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ApiOperation("删除会员等级")
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        int count = memberLevelService.delete(id);
        if(count > 0){
            return CommonResult.success(count);
        }
        return CommonResult.failed("删除会员等级失败");
    }
}
